package core.XML;

import static org.junit.Assert.assertEquals;
import interfaces.GeneratorInterface;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

/**
 * @author Sergey and Julia
 * 
 */
public class TestGeneratorsParsing {
	Document config;
	Document nestedConfig;

	/**
	 * @throws SAXException
	 * @throws IOException
	 */
	@SuppressWarnings("nls")
	@Before
	public void prepare() throws SAXException, IOException {
		DOMParser parser = new DOMParser();
		parser.parse("genTest.xml");
		this.config = parser.getDocument();
		parser.parse("nestedGenTest.xml");
		this.nestedConfig = parser.getDocument();
	}

	/**
	 * 
	 */
	@SuppressWarnings( { "nls", "boxing" })
	@Test
	public void generatorsParsing() {
		Object[] values = { 1, 34, 3.14, 3, 3, 1, "-22" };
		NodeList generators = this.config
				.getElementsByTagName(DefinesXML.GEN_TAG);
		for (int i = 0; i < values.length; ++i) {
			Object scalar = GeneratorsParser.parseGenerator(generators.item(i),
					0)[0].getScalar();
			assertEquals(scalar, values[i]);
		}
	}

	/**
	 * 
	 */
	@SuppressWarnings( { "nls", "boxing" })
	@Test
	public void nestedGeneratorsParsing() {
		Object[] values = { 3, 4, 8, 6, 3 };
		Node genNode = this.nestedConfig.getElementsByTagName(
				DefinesXML.GEN_TAG).item(0); // get first generator

		int i = 0;
		while (true) {
			if (null != genNode
					&& genNode.getNodeName().equals(DefinesXML.GEN_TAG)) {
				GeneratorInterface<?>[] gen = GeneratorsParser.parseGenerator(
						genNode, 0);
				assertEquals(gen[0].getVector().size(), values[i++]);
			}

			if (null == genNode
					|| genNode.equals(this.nestedConfig.getLastChild()))
				break;
			genNode = genNode.getNextSibling();

		}
	}

}
